//
//  SplashController.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/18.
//

import UIKit

import TangramKit

class SplashController: BaseLogicController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initRelativeLayoutSafeArea()
        
        // banner
        let bannerView = UIImageView()
        bannerView.image = R.image.splashBanner()
        bannerView.tg_width.equal(75)
        bannerView.tg_height.equal(309)
        bannerView.tg_top.equal(120)
        bannerView.tg_centerX.equal(0)
        container.addSubview(bannerView)
        
        // 版权
        let copyRightView = UILabel()
        copyRightView.tg_width.equal(.wrap)
        copyRightView.tg_height.equal(.wrap)
        copyRightView.tg_bottom.equal(16)
        copyRightView.tg_centerX.equal(0)
        let year = SuperDateUtil.currentYear()
        copyRightView.text = String(format: "Copyright © %d Ixuea. All Rights Reserved", year)
        copyRightView.font = UIFont.systemFont(ofSize: 12)
        copyRightView.textColor = .gray
        container.addSubview(copyRightView)
        
        // logo
        let logoView = UIImageView(image: R.image.splashLogo())
        logoView.tg_width.equal(188)
        logoView.tg_height.equal(31)
        logoView.tg_bottom.equal(copyRightView.tg_top).offset(16)
        logoView.tg_centerX.equal(0)
        logoView.contentMode = .scaleAspectFit
        container.addSubview(logoView)
        
    }
    
    override func initDatum() {
        super.initDatum()
        if DefaultPreferenceUtil.isAcceptTermsServiceAgreement(){
            //已经同意了用户协议
            prepareNext()
        }else{
            showTermsServiceAgreementDialog()
        }
    }
    
    func prepareNext() {
        next()
    }
    
    func next() {
        //AppDelegate.shared.toMain()
        AppDelegate.shared.toAd()
    }

    /// 显示服务条款对话框
    func showTermsServiceAgreementDialog() {
        dialogController.show()
    }
    
    @objc func primaryClick(){
        dialogController.hide()
        
        AppDelegate.shared.toGuide()
        
        DefaultPreferenceUtil.setAcceptTermsServiceAgreement(true)
    }
    
    lazy var dialogController: TermServiceDialogController = {
        let r = TermServiceDialogController()
        r.titleView.text = R.string.localizable.termServicePrivacy()
        r.primaryButton.addTarget(self, action: #selector(primaryClick), for: .touchUpInside)
        return r
    }()
}
